﻿using Engine;
using Engine.Graphics;
using Engine.Media;
using Game;
using GameEntitySystem;
using TemplatesDatabase;

public class newComponentScreenOverlays : ComponentScreenOverlays, IDrawable
{

    public DynamicArray<ComponentBody> m_componentBodies = new DynamicArray<ComponentBody>();


    public new void Update(float dt)
    {
        bool flag = this.m_subsystemSky.ViewUnderWaterDepth > 0f;
        if (flag != this.m_isUnderWater)
        {
            this.m_isUnderWater = flag;
            this.m_waterSurfaceCrossTime = new double?(this.m_subsystemTime.GameTime);
        }
        base.BlackoutFactor = 0f;
        base.RedoutFactor = 0f;
        base.GreenoutFactor = 0f;
        base.IceFactor = 0f;
        base.FloatingMessage = null;
        base.FloatingMessageFactor = 0f;
        base.Message = null;
        base.MessageFactor = 0f;
        this.Text = false;
        this.Text2 = false;
    }

   
    public new void Draw(Camera camera, int drawOrder)
    {
        if (this.m_componentPlayer.GameWidget != camera.GameWidget)
        {
            return;
        }
        if (this.m_waterSurfaceCrossTime != null)
        {
            float num = (float)(this.m_subsystemTime.GameTime - this.m_waterSurfaceCrossTime.Value);
            float num2 = 0.66f * MathUtils.Sqr(MathUtils.Saturate(1f - 0.75f * num));
            if (num2 > 0.01f)
            {
                Matrix matrix = default(Matrix);
                matrix.Translation = Vector3.Zero;
                matrix.Forward = camera.ViewDirection;
                matrix.Right = Vector3.Normalize(Vector3.Cross(camera.ViewUp, matrix.Forward));
                matrix.Up = Vector3.Normalize(Vector3.Cross(matrix.Right, matrix.Forward));
                Vector3 vector = matrix.ToYawPitchRoll();
                Vector2 zero = Vector2.Zero;
                zero.X -= 2f * vector.X / 3.1415927f + 0.05f * MathUtils.Sin(5f * num);
                zero.Y += 2f * vector.Y / 3.1415927f + (this.m_isUnderWater ? (0.75f * num) : (-0.75f * num));
                Texture2D texture2D = ContentManager.Get<Texture2D>("Textures/SplashOverlay");
                base.DrawTexturedOverlay(camera, texture2D, new Color(156, 206, 210), num2, num2, zero);
            }
        }
        if (base.IceFactor > 0f)
        {
            base.DrawIceOverlay(camera, base.IceFactor);
        }
        if (base.RedoutFactor > 0.01f)
        {
            base.DrawOverlay(camera, new Color(255, 64, 0), MathUtils.Saturate(2f * (base.RedoutFactor - 0.5f)), base.RedoutFactor);
        }
        if (base.BlackoutFactor > 0.01f)
        {
            base.DrawOverlay(camera, Color.Black, MathUtils.Saturate(2f * (base.BlackoutFactor - 0.5f)), base.BlackoutFactor);
        }
        if (base.GreenoutFactor > 0.01f)
        {
            base.DrawOverlay(camera, new Color(166, 175, 103), base.GreenoutFactor, MathUtils.Saturate(2f * base.GreenoutFactor));
        }
        if (!string.IsNullOrEmpty(base.FloatingMessage) && base.FloatingMessageFactor > 0.01f)
        {
            base.DrawFloatingMessage(camera, base.FloatingMessage, base.FloatingMessageFactor);
        }
        if (!string.IsNullOrEmpty(base.Message) && base.MessageFactor > 0.01f)
        {
            base.DrawMessage(camera, base.Message, base.MessageFactor);
        }
        if (!this.Text)
        {
            this.Drawsuiyin(camera, "奶块Mod", 2f);
        }
        if (!this.Text2)
        {
            PlayerData playerData = this.m_componentPlayer.PlayerData;
            this.block = BlocksManager.Blocks[Terrain.ExtractContents(this.m_componentPlayer.ComponentMiner.ActiveBlockValue)];
            string displayName = this.block.GetDisplayName(null, this.m_componentPlayer.ComponentMiner.ActiveBlockValue);
            float meleePower = this.block.GetMeleePower(this.m_componentPlayer.ComponentMiner.ActiveBlockValue);
            float projectilePower = this.block.GetProjectilePower(this.m_componentPlayer.ComponentMiner.ActiveBlockValue);
            float meleeHitProbability = this.block.GetMeleeHitProbability(this.m_componentPlayer.ComponentMiner.ActiveBlockValue);
            int damage = this.block.GetDamage(this.m_componentPlayer.ComponentMiner.ActiveBlockValue);
            int num3 = this.block.Durability - damage;
            Vector3 position = this.m_componentPlayer.ComponentBody.Position;
            int num4 = (int)position.X;
            int num5 = (int)position.Y;
            int num6 = (int)position.Z;
            this.m_componentBodies.Clear();
            SubsystemBodies m_subsystemBodies = m_componentPlayer.Project.FindSubsystem<SubsystemBodies>();
            m_subsystemBodies.FindBodiesAroundPoint(new Vector2(position.X, position.Z), 8f, this.m_componentBodies);
            int SWSL = this.m_componentBodies.Count;
            NK9 hL = m_componentPlayer.Entity.FindComponent<NK9>();
            float num7 = this.m_componentPlayer.ComponentMiner.AttackPower;
            float humidity = this.m_componentPlayer.ComponentHealth.FallResilience;
            int num8 = this.m_subsystemTerrain.Terrain.GetSeasonalTemperature(num4, num6) + SubsystemWeather.GetTemperatureAdjustmentAtHeight(num5);
            double num10 = (this.subsystemGameInfo.TotalElapsedGameTime - playerData.FirstSpawnTime) / 2400.0;
            float num9 = this.m_componentPlayer.ComponentHealth.Health * this.m_componentPlayer.ComponentHealth.AttackResilience;
            string JCJZName = hL.JCJZName;
            string FS;

            if (meleePower > (m_componentPlayer.PlayerData.Level + 10))
            {
                FS = "是";
            }
            else
            {
                FS = "否";
            }

            this.Drawsuiyin2(camera, this.block.Behaviors.Contains("ThrowableBlockBehavior") ? string.Format("位置{0:0}, {2:0}, {1:0} 人物血量:{3:0} 生存天数:{4:0.0}天\n攻击力:{5:0} 摔落抗性:{6} 环境温度:{7}\n名称：{8} 耐久:{9} 近战:{10} 命中率:{11} 远程:{12}\n 副物品栏加成:{13} 生物数量:{14} 反噬:{15}", new object[]
            {
                    position.X,
                    position.Z,
                    position.Y,
                    num9,
                    num10,
                    num7,
                    humidity,
                    num8,
                    displayName,
                    num3,
                    meleePower,
                    meleeHitProbability,
                    projectilePower,
                    JCJZName,
                    SWSL,
                    FS
            }) : string.Format("位置{0:0}, {2:0}, {1:0} 人物血量:{3:0}  生存天数:{4:0.0}天\n攻击力:{5:0} 摔落抗性:{6} 环境温度:{7}\n名称：{8} 耐久:{9} 近战:{10} 命中率:{11}\n 副物品栏加成:{12} 生物数量:{13} 反噬:{14}", new object[]
            {
                    position.X,
                    position.Z,
                    position.Y,
                    num9,
                    num10,
                    num7,
                    humidity,
                    num8,
                    displayName,
                    num3,
                    meleePower,
                    meleeHitProbability,
                    JCJZName,
                    SWSL,
                    FS
            }), 1f);
        }
    }

    
    public override void Load(ValuesDictionary valuesDictionary, IdToEntityMap idToEntityMap)
    {
        this.m_subsystemTime = base.Project.FindSubsystem<SubsystemTime>(true);
        this.subsystemGameInfo = base.Project.FindSubsystem<SubsystemGameInfo>(true);
        this.m_subsystemTerrain = base.Project.FindSubsystem<SubsystemTerrain>(true);
        this.m_subsystemSky = base.Project.FindSubsystem<SubsystemSky>(true);
        this.m_componentGui = base.Entity.FindComponent<ComponentGui>(true);
        this.m_componentPlayer = base.Entity.FindComponent<ComponentPlayer>(true);
    }

    
    public void Drawsuiyin(Camera camera, string message, float factor)
    {
        BitmapFont bitmapFont = ContentManager.Get<BitmapFont>("Fonts/Pericles");
        var position = new Vector2(camera.ViewportSize.X / 2f, 37f);
        FontBatch2D fontBatch2D = this.m_primitivesRenderer2D.FontBatch(bitmapFont, 0, DepthStencilState.None, null, BlendState.AlphaBlend, null);
        int count = fontBatch2D.TriangleVertices.Count;
        fontBatch2D.QueueText(message, position, 0f, new Color(155, 155, 155, 0), TextAnchor.HorizontalCenter | TextAnchor.VerticalCenter, new Vector2(0.85f, 0.85f) * camera.GameWidget.GlobalScale, Vector2.Zero, 0f);
        fontBatch2D.TransformTriangles(camera.ViewportMatrix, count, -1);
        fontBatch2D.Flush(true);
    }

   
    public void Drawsuiyin2(Camera camera, string message, float factor)
    {
        BitmapFont bitmapFont = ContentManager.Get<BitmapFont>("Fonts/Pericles");
        var position = new Vector2(camera.ViewportSize.X / 1.25f, 96f);
        FontBatch2D fontBatch2D = this.m_primitivesRenderer2D.FontBatch(bitmapFont, 0, DepthStencilState.None, null, BlendState.AlphaBlend, null);
        int count = fontBatch2D.TriangleVertices.Count;
        fontBatch2D.QueueText(message, position, 0f, Color.White, TextAnchor.HorizontalCenter | TextAnchor.VerticalCenter, new Vector2(0.6f, 0.6f) * camera.GameWidget.GlobalScale, Vector2.Zero, 0f);
        fontBatch2D.TransformTriangles(camera.ViewportMatrix, count, -1);
        fontBatch2D.Flush(true);

    }
    public bool Text;
    public bool Text2;
    public Block block;
    public string name;
    public SubsystemGameInfo subsystemGameInfo;
}
